package com.ytt.core.api.controller.scores;

import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.model.scores.ScoresDto;
import com.ytt.core.model.scores.ScoresEditDto;
import com.ytt.core.model.scores.ScoresType;
import com.ytt.core.service.service.ProposalQueryParam;
import com.ytt.core.service.service.scores.ScoresService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

@Api(tags = {"分数管理"})
@RestController
@RequestMapping("/api/scores")
@Validated
public class ScoresController {
    private  final ScoresService scoresService;

    @Autowired
    public ScoresController(ScoresService scoresService) {
        this.scoresService = scoresService;
    }

    @ApiOperation(value = "根据ID查找分数")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('SCORES_RETRIEVE')")
    public ScoresDto findById(
            @ApiParam(value = "分数ID", required = true) @PathVariable String id) {
        return scoresService.findById(id);
    }

    @ApiOperation(value = "分页查找分数")
    @GetMapping(value = "")
    @PreAuthorize("hasAnyAuthority('SCORES_RETRIEVE')")
    public PageDataDTO<ScoresDto> findOnePage(
            @ApiParam(value = "页号，从0开始", defaultValue = "0") @RequestParam(value = "page", required = false) @Min(0) Integer page,
            @ApiParam(value = "每页纪录条数", defaultValue = "20") @RequestParam(value = "size", required = false) @Min(1) @Max(100) Integer size,
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(value = "分数名称") @RequestParam(value = "name", required = false) String name) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .build();
        return scoresService.findOnePage(page, size, sort, param);
    }

    @ApiOperation(value = "查找所有分数")
    @GetMapping(value = "/all")
    @PreAuthorize("hasAnyAuthority('SCORES_RETRIEVE')")
    public List<ScoresDto> findAll(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "分数名称") @RequestParam(value = "name", required = false) String name) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .build();
        return scoresService.findAll(sort,param);
    }



    @ApiOperation(value = "创建分数")
    @PostMapping(value = "")
    @PreAuthorize("hasAnyAuthority('SCORES_CREATE')")
    public ScoresDto create(@ApiParam(value = "分数信息", required = true) @RequestBody @Validated ScoresEditDto scoresEditDto) {
        return scoresService.create(scoresEditDto);
    }

    @ApiOperation(value = "更新分数")
    @PutMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('SCORES_UPDATE')")
    public ScoresDto update(@ApiParam(value = "分数ID", required = true) @PathVariable String id,
                                 @ApiParam(value = "分数信息", required = true) @RequestBody @Validated ScoresEditDto scoresEditDto) {
        return scoresService.update(id, scoresEditDto);
    }

    @ApiOperation(value = "删除分数")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('SCORES_DELETE')")
    public void delete(@ApiParam(value = "分数ID", required = true) @PathVariable String id) {
        scoresService.delete(id);
    }

    @ApiOperation(value = "班级通过个数")
    @GetMapping(value = "/class")
    @PreAuthorize("hasAnyAuthority('SCORES_RETRIEVE')")
    public int findPassScoreBySchoolClass(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "班级名称") @RequestParam(value = "name", required = false) String name,
    @ApiParam(value = "分数" ,required = true) @RequestParam(value = "score", required = true) int score) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .schoolClass(name)
                .score(score)
                .build();
        return scoresService.passScoreBySchoolClass(sort,param);
    }


    @ApiOperation(value = "学院通过个数")
    @GetMapping(value = "/college")
    @PreAuthorize("hasAnyAuthority('SCORES_RETRIEVE')")
    public int findPassScoreByCollege(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "学院名称") @RequestParam(value = "name", required = false) String name,@ApiParam(value = "分数" ,required = true) @RequestParam(value = "score", required = true) int score) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .college(name).score(score)
                .build();
        return scoresService.passScoreByCollege(sort,param);
    }

    @ApiOperation(value = "专业通过个数")
    @GetMapping(value = "/major")
    @PreAuthorize("hasAnyAuthority('SCORES_RETRIEVE')")
    public int findPassScoreByMajor(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "专业名称") @RequestParam(value = "name", required = false) String name,@ApiParam(value = "分数" ,required = true) @RequestParam(value = "score", required = true) int score) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .major(name).score(score)
                .build();
        return scoresService.passScoreByMajor(sort,param);
    }

    @ApiOperation(value = "类型通过个数")
    @GetMapping(value = "/type")
    @PreAuthorize("hasAnyAuthority('SCORES_RETRIEVE')")
    public int findPassScoresType(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "类型名称") @RequestParam(value = "name", required = false) ScoresType name,@ApiParam(value = "分数" ,required = true) @RequestParam(value = "score", required = true) int score) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .scoresType(name).score(score)
                .build();
        return scoresService.passScoreByScoresType(sort,param);
    }
}
